package com.hs.servlet;

import com.alibaba.fastjson.JSON;
import com.hs.dao.DaoImpl.GoodsDaoImpl;
import com.hs.entity.Goods;
import com.hs.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/goods")
public class GoodsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        GoodsDaoImpl goodsDao = new GoodsDaoImpl();
        if ("selectAllGoods".equals(action)){
            Map map = new HashMap();
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String goodsName = req.getParameter("goodsName");
            String goodsStatus = req.getParameter("goodsStatus");
            String shopName = req.getParameter("shopName");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");

            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            map.put("goodsName",goodsName);
            map.put("goodsStatus",goodsStatus);
            map.put("shopName",shopName);
            map.put("startTime",startTime);
            map.put("endTime",endTime);

            List<Goods> list = goodsDao.selectAll(map);
            int total = goodsDao.selectCount(map);
            Map resMap = new HashMap();
            resMap.put("list",list);
            resMap.put("total",total);
            Result result = Result.success(resMap);
            resp.getWriter().write(JSON.toJSONString(result));

        } else if ("changeStatus".equals(action)) {
            int goodsId = 0;
            if (req.getParameter("goodsId")!=null){
                goodsId = Integer.parseInt( req.getParameter("goodsId"));
            }

            String goodsStatus = req.getParameter("goodsStatus");
            int num = goodsDao.changeStatus(goodsStatus, goodsId);
            if (num!=0){
                Result success = Result.success("操作成功!");
                resp.getWriter().write(JSON.toJSONString(success));
            }else {
                Result success = Result.error("操作失败!");
                resp.getWriter().write(JSON.toJSONString(success));
            }

        }

        //该写上下架状态

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
